package ControlLayer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Date;

import ModelLayer.*;

/**
 * Handles all changes done to DVDCon container and DVD/Copy objects
 */
public class DVDCtr {
    private int uniqueID = 0;
    private DVDCon dvdCon;

    public DVDCtr() {
        dvdCon = DVDCon.getInstance();
    }

    public DVD findDVD(int id) { //Search by id.
        ArrayList<DVD> tempCon = dvdCon.get(); //get ArrayList of persons.
        for (DVD dvd : tempCon) {
            if (dvd.getId() == id) return dvd;
        }
        return null;
    }

    public DVD findDVD(String name) { //Search by name.
        ArrayList<DVD> tempCon = dvdCon.get(); //get ArrayList of persons.
        for (DVD dvd : tempCon) {
            if (dvd.getTitle().contains(name)) return dvd;
        }
        return null;
    }

    public void createDVD(String title, String artist) {
        DVD dvd = new DVD(++uniqueID, title, artist);
        dvdCon.add(dvd);
    }

    public void createCopy(int dvdID, double price) {
        boolean searching = true;
        Copy copy;
        copy = new Copy(findDVD(dvdID).stockSize(), null, price);
        ArrayList<DVD> dvdList = dvdCon.get(); //get ArrayList of DVDs
        for (int indx = 0; indx < dvdList.size() && searching; indx++) { //Sweap for a match and updates it.
            if (dvdList.get(indx).getId() == dvdID) {
                DVD dvd = dvdList.get(indx);
                dvd.addCopy(copy);
                dvdList.set(indx, dvd);
                searching = false;
            }
        }
        dvdCon.set(dvdList);
    }

    public ArrayList listAllDVDs() {
        return dvdCon.get();
    }
/*
    public void deleteDVD(int id) {
        dvdCon.removeById(id);
        //search for the dvd and delete the object from the container

    }*/
    /*

    public void createCopy(int id)
    {
    DVD d = dvdCon.getById(id);

    Copy c = new Copy();
    c.setPurchaseDate();
    c.setPurchasesPrice();
    c.setSerialNumber();

    //create a copy of the specified dvd and add it to the dvd
    }

    public ArrayList listAllDvds()
    {
    //returns a list of all dvds and their associated copies
    return null;
    }*/
}
